<!DOCTYPE html>
<html lang="en">

<head>
    <script src="../utils/jquery3.6.js"></script>
    <script src="../utils/template.js"></script>
    <link rel="stylesheet" href="../css/navmove.css">
    <link rel="stylesheet" href="../css/swiper.min.css">
    <link rel="stylesheet" href="../assets/iconFonts/iconfont.css">
    <link rel="stylesheet" href="../css/baicaijia.css">
    <script src="../utils/flexible.js"></script>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>baicaijia</title>
    <script src="../js/backtop.js"></script>
    <script src="../js/render.js"></script>
</head>

<body>
    <div class="header">
        <a href="../index.html">
            <</a> <h4>白菜价-淘宝内部优惠卷</h4>
                <div>
                    <a href="">
                        <img src="../assets/images/header_app.png" alt="">
                    </a>
                </div>
    </div>
    <div class="nav">
        <div class="swiper-container nav" id="nav">
            <div class="swiper-wrapper nav-list">
            </div>
        </div>
    </div>
    <div class="product">
        <ul class="info">

        </ul>
    </div>
    <section class="footer">
        <div class="footmore ">
            <a href="">更多优惠信息>>></a>
        </div>
        <div class="chart clear">
            <span>品牌排行</span>
            <span><img src="../assets/images/more.png" alt=""></span>
        </div>
        <div class="login">
            <div class="foottop">
                <div class="footrow clear">
                    <a href="../index.html">登入</a>
                    <a href="../index.html">注册</a>
                    <a href="">返回顶部</a>
                </div>
            </div>
            <div class="footbottom">
                <p>
                    <a href="">手机app下载</a>
                    <a href="">慢慢买手机版</a>
                    -- 掌上比价平台
                </p>
                <p>m.manmanbuy.com</p>
            </div>
        </div>
    </section>
</body>

</html>
<script>
    $(function () {
        function get_menu() {
            $.ajax({
                url: 'http://chst.vip:1234/api/getbaicaijiatitle',
                success: function (data) {
                    var str = '';
                    $.each(data.result, function (k, v) {
                        /* console.log(v.title);
                        console.log(k); */
                        v.cate_name = v.title;
                        if (k == 0) {
                            str += ' <div class="swiper-slide nav-a selected" navid="' +
                                v.titleId + '"><span>' + v.title + '</span></div>';
                        } else {
                            str += ' <div class="swiper-slide nav-a" navid="' + v.titleId +
                                '"><span>' + v.title + '</span></div>';
                        }

                    })
                    str += ' <div class="bar"><div class="color"></div></div>';
                    $('#nav .swiper-wrapper').html(str);
                }
            });
        };
        get_menu()
        //点击导航高亮
        $("body").on("click", '.nav-a', function () {
            var moveX = $(this).position().left + $(this).parent().scrollLeft();
            var pageX = document.documentElement.clientWidth;
            var blockWidth = $(this).width();
            var left = moveX - (pageX / 2) + (blockWidth / 2);
            $(".nav-list").animate({
                scrollLeft: left,
            });
            $(".nav-a").removeClass("selected");
            $(this).addClass("selected");
            var cate_index = $(this).prevAll().length;
            var a_id = $(this).attr('navid');
            if (a_id == 0) {
                $('.tuijian').show();
                $('#qudao').show();
            } else {
                $('.tuijian').hide();
                $('#qudao').hide();
            }
            var pxs = parseInt(cate_index) * 52;
            $('.bar').css({
                "width": "52px",
                "transition-duration": "100ms",
                "transform": "translateX(" + pxs + "px)"
            })
        });
        //页面首次渲染调用render函数
        render({
            url: 'http://chst.vip:1234/api/getbaicaijiaproduct',
            data: {
                titleid: 0
            },
            callfn: function (data) {
                callback(data)
            }
        })
        //处理数据函数
        function callback(data) {
            console.log(data);
            let html = template('product', data)
            $('.info').html(html)
            navrender()
            bar()
        }
        //nav点击渲染
        function navrender() {
            $('.swiper-slide').click(function () {
                //attr获取人为设置的属性，prop获取自身携带的属性
                render({
                    url: 'http://chst.vip:1234/api/getbaicaijiaproduct',
                    data: {
                        titleid: $(this).attr('navid')
                    },
                    callfn: function (data) {
                        callback(data)
                    }
                })
                //在每次调用jquery绑定函数时，并不会覆盖之前对于元素绑定，而是会增加，
                //所以说，1-2，2-4，4-8… 绑定的ajax请求自然会翻倍，如果想避免这种情况的话，
                //可以使用，$(‘element’).unbind()进行解绑，然后再进行新的绑定就好了。
                $('.swiper-slide').unbind()
            })
        }

        function bar() {
            //bar动画
            let t = parseInt( $('.bar2').width())-13
            console.log(t);
            let timer = setInterval(function () {
                t--
                console.log(t);
               $('.bar_in').width(($('.bar2').width()-t)+'px')
               $('.bar_in').text(`${parseInt($('.bar2').width()-18-t)}%`)
                if (t <0) {
                    clearInterval(timer)
                }
            }, 100)
        }

    })
</script>
<script type="text/html" id="product">
    {{each result value i}}
    <li>
        <a href="" class="clear">
            <div class="img fl">
                {{@value.productImg}}
            </div>
            <div class="infor">
                <div class="info-title">
                    <h4>
                        {{@value.productName}}
                    </h4>
                    <div class="price">
                        {{@value.productPrice}}
                    </div>
                </div>
                <div class="down">
                    <div class="bar2">
                        <div class="bar_in">100%</div>
                    </div>
                    <span class="yiling">已领126张/剩余0张</span>
                    <div class="click">
                        <div class="clickleft">点此领取优惠卷</div>
                        <div class="clickright">下单链接</div>
                    </div>
                </div>
            </div>
        </a>
    </li>
    {{/each}}
</script>